package com.example.shixun1.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.shixun1.R;
import com.example.shixun1.adapter.CartAdapter;
import com.example.shixun1.util.CartManager;
import java.util.Locale;

public class CartActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private CartAdapter adapter;
    private TextView tvTotalPrice;
    private Button btnCheckout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        recyclerView = findViewById(R.id.rv_cart);
        tvTotalPrice = findViewById(R.id.tv_total_price);
        btnCheckout = findViewById(R.id.btn_checkout);

        setupRecyclerView();
        updateTotalPrice();

        btnCheckout.setOnClickListener(v -> {
            // 结算逻辑
            CartManager.getInstance().clearCart();
            finish();
        });
    }

    private void setupRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CartAdapter(CartManager.getInstance().getCartItems(),
                () -> updateTotalPrice());
        recyclerView.setAdapter(adapter);
    }

    private void updateTotalPrice() {
        double total = CartManager.getInstance().getTotalPrice();
        tvTotalPrice.setText(String.format(Locale.getDefault(), "总计: ¥%.2f", total));
    }
}